<template>
  <div>
    <div class="top-box">
      <el-button type="success" @click="add">添加推荐配置</el-button>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
        <el-select v-model="searchParam.item_type" placeholder="一级分类" class="cycle-input"  @change="searchChangeFirstLevel">
          <el-option key="" label="请选择一级分类" value=""></el-option>
          <el-option
            v-for="item in firstLevelCategoryList"
            :key="item.key"
            :label="item.value"
            :value="item.key">
          </el-option>
        </el-select>
        <el-select v-model="searchParam.item_sub_type" placeholder="二级分类" class="cycle-input" @change="searchChangeSecondLevel">
          <el-option key="" label="请选择二级分类" value=""></el-option>
          <el-option
            v-for="item in secondLevelCategoryList"
            :key="item.key"
            :label="item.value"
            :value="item.key">
          </el-option>
        </el-select>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="一级分类" prop="item_type_name"></el-table-column>
        <el-table-column label="二级分类" prop="item_sub_type_name"></el-table-column>
        <el-table-column label="编号(文章/视频/作者/专栏)" prop="item_id"></el-table-column>
        <el-table-column label="添加时间" prop="add_time"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <!--<i class="i-icon icon-check" title="查看" @click="viewDetail(scope.row.id)"></i>-->
            <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="添加推荐配置" :visible.sync="showAddModelFlag" size="small">
      <div class="input-group">
        <label><i>*</i>推荐位置：</label>
        <div class="input-group-box">
          <el-select v-model="addParam.item_type" placeholder="一级分类" class="cycle-input"  @change="addChangeFirstLevel">
            <el-option
              v-for="item in firstLevelCategoryList"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
          <el-select v-model="addParam.item_sub_type" placeholder="二级分类" class="cycle-input" @change="addChangeSecondLevel">
            <el-option
              v-for="item in secondLevelCategoryList"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
        </div>
      </div>
	    <div class="input-group">
        <label><i>*</i>项目编号(文章/视频/作者/专栏)：</label>
        <div class="input-group-box">
          <el-input v-model="addParam.item_id" placeholder="请输入项目编号" type="number"></el-input>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>排序：</label>
        <div class="input-group-box">
          <el-input v-model="addParam.sort" placeholder="请输入排序"  type="number"></el-input>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改推荐配置" :visible.sync="showUpdateModelFlag" size="small">
      <div class="input-group">
        <label><i>*</i>推荐位置：</label>
        <div class="input-group-box">
          <el-select v-model="editParam.item_type" placeholder="请选择一级分类" class="cycle-input"  @change="editChangeFirstLevel">
            <el-option
              v-for="item in firstLevelCategoryList"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
          <el-select v-model="editParam.item_sub_type" placeholder="二级分类" class="cycle-input" @change="editChangeSecondLevel">
            <el-option
              v-for="item in secondLevelCategoryList"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
        </div>
      </div>
	    <div class="input-group">
        <label><i>*</i>项目编号(文章/视频/作者/专栏)：</label>
        <div class="input-group-box">
          <el-input v-model="editParam.item_id" placeholder="请输入项目编号"  type="number"></el-input>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>排序(越大越靠前)：</label>
        <div class="input-group-box">
          <el-input v-model="editParam.sort" placeholder="请输入排序"  type="number"></el-input>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      firstLevelCategoryList: [],
      secondLevelCategoryList: [],
      searchParam: {
        id: '',
        item_type: '',
        item_sub_type: '',
        item_id: ''
      },
      addParam: {
        item_type: '',
        item_sub_type: '',
        item_id: '',
        sort: 0
      },
      editParam: {
        id: '',
        item_type: '',
        item_sub_type: '',
        item_id: '',
        sort: 0
      }
    }
  },
  created () {
    this.getList()
    this.getCategoryLevelOption()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        item_type: this.searchParam.item_type,
        item_sub_type: this.searchParam.item_sub_type,
        item_id: this.searchParam.item_id
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.homeConfigList), this.params, res => {
        this.listData = res
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.item_type = ''
      this.searchParam.item_sub_type = ''
      this.searchParam.item_id = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    add () {
      this.showAddModelFlag = true
    },
    confirmAdd () {
      if (this.addParam.item_type.length === 0) {
        this.noEmpty('请选择一级分类')
        return false
      }
      if (this.addParam.item_sub_type.length === 0) {
        this.noEmpty('请选择二级分类')
        return false
      }
      if (this.addParam.item_id <= 0) {
        this.noEmpty('项目编号必须大于0')
        return false
      }
      var param = {
        item_type: this.addParam.item_type,
        item_sub_type: this.addParam.item_sub_type,
        item_id: this.addParam.item_id,
        sort: this.addParam.sort
      }
      api.GET(helper.getManageApiUrl(apiConfig.addHomeConfig), param, res => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.addParam.item_type = ''
        this.addParam.item_sub_type = ''
        this.addParam.item_id = ''
        this.addParam.sort = 0
        this.showAddModelFlag = false
        this.getList()
      }, res => {
        console.log(res)
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'fail',
          duration: 1500
        })
      })
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        api.GET(helper.getManageApiUrl(apiConfig.deleteHomeConfig), param, res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      })
      .catch(_ => {})
    },
    edit (item) {
      this.editParam.id = item.id
      this.editParam.item_type = item.item_type
      this.editParam.item_sub_type = item.item_sub_type
      this.editParam.item_id = item.item_id
      this.editParam.sort = item.sort
      this.showUpdateModelFlag = true
    },
    save () {
      if (this.editParam.item_type.length === 0) {
        this.noEmpty('请选择一级分类')
        return false
      }
      if (this.editParam.item_sub_type.length === 0) {
        this.noEmpty('请选择二级分类')
        return false
      }
      if (this.editParam.item_id === 0) {
        this.noEmpty('项目编号必须大于0')
        return false
      }
      api.GET(helper.getManageApiUrl(apiConfig.updateHomeConfig), this.editParam, res => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1500
        })
        this.editParam.item_type = ''
        this.editParam.item_sub_type = ''
        this.editParam.item_id = ''
        this.editParam.sort = 0
        this.showUpdateModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '修改失败',
          message: res.msg,
          type: 'error',
          duration: 1500
        })
      })
    },
    getCategoryLevelOption () {
      var param = {
        param: ''
      }
      api.GET(helper.getManageApiUrl(apiConfig.homeConfigRecommendItems), param, res => {
        this.firstLevelCategoryList = res
      }, res => {
        console.log(res)
      })
    },
    searchChangeFirstLevel (val) {
      this.changeFirstLevel(val, 'search')
    },
    searchChangeSecondLevel (val) {
      this.changeSecondLevel(val, 'search')
    },
    addChangeFirstLevel (val) {
      this.changeFirstLevel(val, 'add')
    },
    addChangeSecondLevel (val) {
      this.changeSecondLevel(val, 'add')
    },
    editChangeFirstLevel (val) {
      this.changeFirstLevel(val, 'edit')
    },
    editChangeSecondLevel (val) {
      this.changeSecondLevel(val, 'edit')
    },
    // 一级分类的更改
    changeFirstLevel (val, type) {
      console.log('val====>' + val)
      if (type === 'add') {
        this.addParam.item_type = val
      } else if (type === 'edit') {
        this.editParam.item_type = val
      } else if (type === 'search') {
        this.searchParam.item_type = val
      }
      this.secondLevelCategoryList = []
      this.firstLevelCategoryList.forEach((item) => {
        if (item.key === val) {
          this.secondLevelCategoryList = item.list
        }
      })
    },
    // 二级分类的更改
    changeSecondLevel (val, type) {
      if (type === 'add') {
        this.addParam.item_sub_type = val
      } else if (type === 'edit') {
        this.editParam.item_sub_type = val
      } else if (type === 'search') {
        this.searchParam.item_sub_type = val
      }
    },
    noEmpty (tip) {
      this.$notify({
        title: '提示',
        message: tip,
        type: 'error',
        duration: 1500
      })
    }
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }
</style>

